using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
/// <summary>
///
/// </summary>
public class Class1
{
   /// <summary>
   ///
   /// </summary>
   /// <param name="context">httpcontext</param>
   /// <param name="sessionKey">SESSION KEY</param>
   public void OutputImage(System.Web.HttpContext context, string sessionKey)
   {
      string expression = null;
      string allChar = "0,1,2,3,4,5,6,7,8,9";
      string[] allCharArray = allChar.Split(',');
      int temp;
      Random rnd = new Random();
      for (int i = 0; i < 4; i++)
      {
         int t = rnd.Next(10);
         temp = t;
         expression += allCharArray[t];
      }
      using (Bitmap bmp = new Bitmap(60, 25))
      {
         using (Graphics graph = Graphics.FromImage(bmp))
         {
            graph.Clear(Color.FromArgb(232, 238, 247));
            for (int i = 0; i <= 128; i++)
            {
               graph.DrawRectangle(
                  new Pen(Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255))),
                  rnd.Next(2, 128),
                  rnd.Next(2, 38),
                  1,
                  1);
            }
            for (int i = 0; i < expression.Length; i++)
            {
               graph.DrawString(expression.Substring(i, 1),
                  new Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold),
                  new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(128), rnd.Next(255))),
                  5 + i * 10,
                  rnd.Next(1, 5));
            }
            graph.DrawRectangle(new Pen(Color.Firebrick), 0, 0, 60 - 1, 25 - 1);
         }
         context.Session[sessionKey] = expression;
         DisableHttpCache(context);
         context.Response.ContentType = "image/gif";
         bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
         context.Response.End();
      }
   }
   private static void DisableHttpCache(System.Web.HttpContext context)
   {
      context.Response.ClearHeaders();
      context.Response.ClearContent();
      context.Response.AddHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT");
      context.Response.AddHeader("Cache-Control", "no-store, no-cache, max-age=0, must-revalidate");
      context.Response.AddHeader("Pragma", "no-cache");
   }
   public Class1()
   {}
}
